package com.ocpsoft.pretty.time.i18n.impl;

import com.ocpsoft.pretty.time.i18n.I18N;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Retrieves {@link com.ocpsoft.pretty.time.i18n.I18N} values from a specific file.
 *
 * @author Mihai CAZACU (cazacugmihai@yahoo.com)
 */
public class I18NFile implements I18N {

    private Properties props;

    /**
     * Create a class instance using the specified i18n file.
     *
     * @param filePath - the i18n file path
     * @throws IOException
     */
    public I18NFile(String filePath) throws IOException {
        this(new File(filePath));
    }

    /**
     * Create a class instance using the specified i18n file.
     *
     * @param file - the i18n file
     * @throws IOException
     */
    public I18NFile(File file) throws IOException {
        if (file == null || !file.exists()) {
            throw new IllegalArgumentException(
                    "The specified file doesn't exists.");
        }
        props = new Properties();
        props.load(new FileInputStream(file));
    }

    /**
     * Create a class instance using the specified i18n input stream.
     *
     * @param is - the i18n input stream
     * @throws IOException
     */
    public I18NFile(InputStream is) throws IOException {
        props = new Properties();
        props.load(is);
    }

    public String getMessage(String key) {
        return props.getProperty(key, "");
    }

}
